package com.usian.user.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;


public class YongYouYunUtils {




      // 第三方接口  //配置您申请的KEY
    // 身份证OCR
    private static final String IDCARDOCR = "151143623ca946159de11f2424d7edcc ";
    //  身份证
    private static final String IDENTITYAPICODE = "192cf8b29d27459cae58a3ccb201e1e7 ";
    //  活体检测
    private static final String  Biologicalexamination="b038fd1918a24505af9726c5ec512392";
    // 6.人证合一
    private static final String  syncretic="01274f95edac48b98d6bf1a11f69955d ";


    // 人证合一
    public static Map<String, String> ComparisonsofPersonsandDocuments (Map<String, String> params){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode",syncretic);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
//        Map<String,String> map = new HashMap<>();
//        map.put("image","https://hmtt122.oss-cn-shanghai.aliyuncs.com/demo_idcard.png");
//        map.put("imageType","URL");
//        map.put("ocrType","0");
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders); // 封装请求参数
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/ComparisonsofPersonsandDocuments/ComparisonsofPersonsandDocuments", formEntry, String.class);// 发送一个post请求
        System.out.println(response);
        JSONObject object = JSONObject.parseObject(response.getBody());
        JSONObject data = object.getJSONObject("data");
        JSONObject code = object.getJSONObject("code");
        Map<String,String> map= (Map<String, String>) JSON.parse(data.toString());
        map.put("code",code.toString());
        return map ;
    }



    // 活体检测
    public static Map<String, String> bioassay (Map<String, String> params){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode",Biologicalexamination);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
//        Map<String,String> map = new HashMap<>();
//        map.put("image","https://hmtt122.oss-cn-shanghai.aliyuncs.com/demo_idcard.png");
//        map.put("imageType","URL");
//        map.put("ocrType","0");
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders); // 封装请求参数
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/Biologicalexamination/Biologicalexamination",formEntry, String.class);// 发送一个post请求
        System.out.println(response);
        // 只要分数
        JSONObject object = JSONObject.parseObject(response.getBody());
        JSONObject data = object.getJSONObject("data");
        JSONObject code = object.getJSONObject("code");
        Map<String,String> map= (Map<String, String>) JSON.parse(data.toString());
        map.put("code",code.toString());
        return map  ;
    }



    // 身份证OCR
    public static Map<String, String> IdcardOCR(Map<String, String> params){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode",IDCARDOCR);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
//        Map<String,String> map = new HashMap<>();
//        map.put("image","https://hmtt122.oss-cn-shanghai.aliyuncs.com/demo_idcard.png");
//        map.put("imageType","URL");
//        map.put("ocrType","0");
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders); // 封装请求参数
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/IdcardOCR/IdcardOCR", formEntry, String.class);// 发送一个post请求
        JSONObject object = JSONObject.parseObject(response.getBody());
        JSONObject data = object.getJSONObject("data");
        JSONObject code = object.getJSONObject("code");
        Map<String,String> map= (Map<String, String>) JSON.parse(data.toString());
        map.put("code",code.toString());
        return map ;
    }

    /**
     * params.put("idNumber", "?"); 身份证号
     * params.put("userName", "?"); 身份证姓名
     * @return
     * 身份证二要素验证
     */
    @Test
    public static Map<String, String> matchIdentity(Map<String,String> params){
        // 路径  头信息   请求体   请求方式   返回值
        RestTemplate restTemplate = new RestTemplate();
        //头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        // 内容类型  编码格式
        httpHeaders.add("apicode",IDENTITYAPICODE);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 设置请求参数
//        Map<String,String> params = new HashMap<>();
//        params.put("idNumber", "210103195103222113");
//        params.put("userName", "王东镇");
        //设置请求体
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/matchIdentity/matchIdentity",formEntry,String.class);
        JSONObject object = JSONObject.parseObject(response.getBody());
        JSONObject code = object.getJSONObject("code");
        Map<String,String> map= (Map<String, String>) JSON.parse(code.toString());
        return map ;
    }

}
